﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.AdvTree;

namespace LL.DevComponents
{
    public static class AdvTreeExtensions
    {
        public static Node FindNodeOnPath(this AdvTree tree, string nodePath)
        {
            return FindNodeOnPath(tree.Nodes, nodePath, tree.PathSeparator);
        }

        private static Node FindNodeOnPath(NodeCollection tn, string nodepath, string pathSeparator)
        {
            string str1 = null;
            string str2 = null;
            if (tn.Count < 1)
                return null;
            Node TempTn = null;
            int pos = nodepath.IndexOf(pathSeparator, 0);
            if (pos > 0)
            {
                //当前要找的结点  
                str1 = nodepath.Substring(0, pos);
                //下一级目录的结点  
                str2 = nodepath.Substring(pos + 1);
            }
            else
                str1 = nodepath;
            //从当前一级树目录中查找结点  
            for (int i = 0; i < tn.Count; i++)
            {
                //找到了  
                if (tn[i].Text == str1)
                {
                    TempTn = tn[i];
                    //是要找树结点的最终位置  
                    if (str2 == null)
                        break;
                    else
                    {
                        //继续往下查找  
                        Node tnd = FindNodeOnPath(tn[i].Nodes, str2, pathSeparator);
                        if (tnd != null)
                            TempTn = tnd;
                    }
                    break;
                }
            }
            //没找到返回空 
            if (TempTn != null)
            {
                if (TempTn.Parent != null)
                    TempTn.Parent.Expand();
            }
            return TempTn;
        }
    }
}
